<?php
/**
 * @Author: xuzhiyi
 * @Date:   2016-02-16 20:57:17
 * @Last Modified by:   xuzhiyi
 * @Last Modified time: 2016-02-28 13:38:58
 * ------------------------------------------------ *
 * 公共基础模型                                          *
 * ------------------------------------------------ */

namespace Admin\Model;
use \Think\Model;
class CommonModel extends Model {

	/* 用来处理内容中为空的判断
     * --------------------------- */
	public function checkEmpty($data){
	    foreach ($data as $key=>$v){
			if(trim($v)==''){
				return false;
			}
		}
		return true;
	}

	/* 单文件用于上传头像
	 * ------------------------*/
    public function upAvatar($file="managerPhoto"){
		$upAvatar = new \Think\Upload();// 实例化上传类
		$upAvatar->maxSize = 3145728 ;// 设置附件上传大小
		$upAvatar->exts = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
		$upAvatar->rootPath = './Uploads/'; // 设置附件上传根目录
		$upAvatar->subName  =  'avatar';
		$upAvatar->saveName = date('Ymd').uniqid().mt_rand(0,999);
		// 上传单个文件
		$info = $upAvatar->uploadOne($_FILES[$file]);
		if(!$info) {// 上传错误提示错误信息

			//$this->error($upAvatar->getError());
			return false;
		}else{// 上传成功 获取上传文件信息
			$image=new \Think\Image();
			$image->open($upAvatar->rootPath.$info['savepath'].$info['savename']);
			$smallName=str_replace('.','_s.',$info['savename']);
			$middleName=str_replace('.','_m.',$info['savename']);
			$largeName=str_replace('.','_l.',$info['savename']);
			
			
			$image->thumb(350, 350,\Think\Image::IMAGE_THUMB_CENTER)->save($upAvatar->rootPath.$info['savepath'].$largeName);
			$image->thumb(200, 200,\Think\Image::IMAGE_THUMB_CENTER)->save($upAvatar->rootPath.$info['savepath'].$middleName);
			$image->thumb(29, 29,\Think\Image::IMAGE_THUMB_CENTER)->save($upAvatar->rootPath.$info['savepath'].$smallName);
			return $info['savepath'].$info['savename'];
		}
	}

	public function delAvatar($managerPhoto){
		@unlink($_SERVER['DOCUMENT_ROOT'].__UPLOAD__.'/'.str_replace('.','_s.',$managerPhoto));
		@unlink($_SERVER['DOCUMENT_ROOT'].__UPLOAD__.'/'.str_replace('.','_m.',$managerPhoto));
		@unlink($_SERVER['DOCUMENT_ROOT'].__UPLOAD__.'/'.str_replace('.','_l.',$managerPhoto));
		@unlink($_SERVER['DOCUMENT_ROOT'].__UPLOAD__.'/'.$managerPhoto);
	}


    /* 分页查询
	 * ------------------------*/
    public function PQ($sql,$page = 0,$pageSize = 0){
		$pageSize = (intval($pageSize)==0)?C('PAGE_SIZE'):$pageSize;
		if($pageSize==0)return array();
		$page = (intval($page)<=0)?I(C('VAR_PAGE'),1):intval($page);
		$page = ($page<=0)?1:$page;
		$start = ($page-1)*$pageSize;
		$PQ = array();
		$result = array();
		//查询总数
		$totalSql = "select count(*) counts from (".$sql.") as a";	
		$total = $this->query($totalSql);
		//查询数据		
		$result = $this->query($sql." limit ".$start.",".$pageSize);
		//计算页码信息
		$PQ['total'] = $total[0]['counts'];
		$PQ['pageSize'] = $pageSize;
		$PQ['start'] = $start;
		$PQ['list'] = $result;
		$PQ['totalPage'] = ($PQ['total']%$pageSize==0)?($PQ['total']/$pageSize):(intval($PQ['total']/$pageSize)+1);
		$PQ['currPage'] = $page;
		return $PQ;
    }
}